//
//  NoteEditViewVc-CollectionView.swift
//  xiaofenshu
//
//  Created by zhu jinlong on 2021/5/21.
//

import AVKit
import YPImagePicker
import SKPhotoBrowser

extension NoteEditViewVc:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        phoneCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : PhoneCell = collectionView .dequeueReusableCell(withReuseIdentifier: phoneCellId, for: indexPath) as! PhoneCell
        cell.phoneView.image = phones[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionFooter {
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: phoneAddViewId, for: indexPath) as! PhoneAddView
            footerView.showPickerView .addTarget(self, action: #selector(showPickerAction), for: .touchUpInside)
            return footerView
        }else {
            fatalError("header")
        }
    }
}

extension NoteEditViewVc:UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if isVideo {
            let playVc = AVPlayerViewController()
            playVc.player = AVPlayer(url:videoUrl!)
            present(playVc, animated: true) {
                playVc.player?.play()
            }
        }else {
            var browsers:[SKPhoto] = []
            for image in phones {
                browsers.append(SKPhoto.photoWithImage(image))
            }
            
            let browser = SKPhotoBrowser.init(photos: browsers,initialPageIndex: indexPath.item)
            browser.delegate = self
            SKPhotoBrowserOptions.displayAction = false
            SKPhotoBrowserOptions.displayDeleteButton = true
            present(browser, animated: true, completion: nil)
        }

    }
}



extension NoteEditViewVc:SKPhotoBrowserDelegate {
    func removePhoto(_ browser: SKPhotoBrowser, index: Int, reload: @escaping (() -> Void)) {
        phones.remove(at: index)
        phoneCollectionView.reloadData()
        reload()
    }
}

// MARK: - action
extension NoteEditViewVc {
    @objc private func showPickerAction(){
        if phoneCount < kMaxPhoneCount {
            let picker = YPImagePicker(configuration: ImagePickerConfig())
            picker.didFinishPicking {[unowned picker](items, cancelled) in
                if cancelled {
                    print("取消了")
                }
                for item in items {
                    ///if case let   https://www.jianshu.com/p/c8e3affe7caf
                    if case let .photo(p:phone) = item {
                        self.phones.append(phone.image)
                    }
                }
                self.phoneCollectionView.reloadData()
                
                
                picker.dismiss(animated: false, completion: nil)
            }
            
            present(picker, animated: true, completion: nil)
        }else {
            showToast(title: "最多选择\(kMaxPhoneCount)张照片哦")
        }

    }
    
    
    func ImagePickerConfig() -> YPImagePickerConfiguration {
        var config = YPImagePickerConfiguration()
        config.albumName = Bundle.main.appName
        config.screens = [.library]
        
        // MARK: 相册配置
        config.library.mediaType = .photo
        config.library.defaultMultipleSelection = true
        config.library.maxNumberOfItems = kMaxPhoneCount - phoneCount
        config.library.spacingBetweenItems = kSpacingBetweenItems
        
        
        // MARK: 画廊展示
        config.gallery.hidesRemoveButton = false
        
        return config
    }
}
